home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 29 (1992-07)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].zip / MegaDisc 29 (1992-07)(MegaDisc Digital Publishing)(AU)(Disk 1 of 2)[WB].adf / Programming / Basic_Tutorial_10 / Hex-bin-hex.bas < prev    next >
BASIC Source File  |  1992-08-01  |  4KB  |  134 lines

  1. REM $OPTION Y+,FDH0:Progs/hex-bin-hex
  2. SCREEN 1,640,256,2,2
  3. WINDOW 1,,,,1
  4. REM These are the two SHARED variables for the SUB Programs and they
  5. REM are needed so that the converted numbers can be used in the main program.
  6.  
  7. BinNum$="":HexNum$=""
  8.  
  9. REM**************************************************************************
  10.  
  11. REM This is the main program.
  12.  
  13. Main:
  14. CLS
  15. LOCATE 10,15:PRINT "What would you like to do."
  16. LOCATE 12,17:COLOR 1,0
  17. PRINT "(1)    ";:COLOR 3,0:PRINT "Convert from Hex to Binary."
  18. LOCATE 13,17:COLOR 1,0
  19. PRINT "(2)    ";:COLOR 3,0:PRINT "Convert from Binary to Hex."
  20. LOCATE 14,17:COLOR 1,0
  21. PRINT "(3)    ";:COLOR 3,0:PRINT "HELP."
  22. LOCATE 15,17:COLOR 1,0
  23. PRINT "(4)    ";:COLOR 3,0:PRINT "Finished Thank You."
  24. LOCATE 17,15:PRINT "Select a number ";:COLOR 1,0:PRINT "1,2 3 or 4 "
  25.  
  26. Aloop:
  27. K$=INKEY$
  28. IF K$ <"1" OR K$>"4" THEN Aloop
  29. IF K$ ="1" THEN GOTO HexConvert
  30. IF K$ ="2" THEN GOTO BinConvert
  31. IF K$ ="3" THEN GOTO Help
  32. IF K$ ="4" THEN SYSTEM
  33. GOTO Aloop
  34. HexConvert:
  35. CLS:LOCATE 10,25
  36. COLOR 3,0
  37. PRINT "Enter a HEX number ";
  38. COLOR 1,0
  39. INPUT hexa$
  40. PRINT
  41. hexa$=UCASE$(hexa$)
  42. HEXBIN(hexa$)
  43. LOCATE 12,20
  44. PRINT "The Binary Equivalent of ";:COLOR 3,0:PRINT "&H";hexa$;:COLOR 1,0:PRINT " is ";:COLOR 3,0:PRINT "&B";BinNum$
  45. LOCATE 22,20:COLOR 1,0:PRINT "Would you like another [Y]es or [N]"
  46. Dloop:
  47. K$=INKEY$
  48. IF K$="" THEN Dloop
  49. IF K$="Y" OR K$="y" THEN HexConvert
  50. IF K$="N" OR K$="n" THEN Main
  51. GOTO Dloop
  52.  
  53. BinConvert:
  54. CLS:PRINT:PRINT
  55. COLOR 3,0:LOCATE 10,10
  56. PRINT "Enter a binary number (i.e '0100' '11101111') ";
  57. COLOR 1,0
  58. INPUT bina$
  59. BINHEX(bina$)
  60. LOCATE 12,15
  61. PRINT "The Hexadecimal Equivalent of ";:COLOR 3,0:PRINT "&b";bina$;:COLOR 1,0:PRINT " is ";:COLOR 3,0:PRINT "&H"HexNum$
  62. LOCATE 22,20:COLOR 1,0:PRINT "Would you like another [Y]es or [N]"
  63. Bloop:
  64. K$=INKEY$
  65. IF K$="" THEN Bloop
  66. IF K$="Y" OR K$="y" THEN BinConvert
  67. IF K$="N" OR K$="n" THEN Main
  68. GOTO Bloop
  69.  
  70. Help:
  71. CLS:PRINT
  72. PRINT "A Hexadecimal number can be any of the following numbers or letters."
  73. PRINT "0 1 2 3 4 5 6 7 8 9 A B C D E or F"
  74. PRINT "Representing the numbers from 0 to 15"
  75. PRINT "A Hexadecimal number is preceeded by the characters '&H'"
  76. PRINT "So &HFF is equal to 255 in decimal notation"
  77. PRINT
  78. PRINT "Each of the Hexadecimal numbers is represented in Binary by a group of four "
  79. PRINT "characters consisting of '0' or '1' as follows:-"
  80. PRINT
  81. PRINT "     DECIMAL   HEX     BINARY    DECIMAL    HEX    BINARY"
  82. PRINT "        0       0       0000        8        8      1000"
  83. PRINT "        1       1       0001        9        9      1001"
  84. PRINT "        2       2       0010       10        A      1010"
  85. PRINT "        3       3       0011       11        B      1011"
  86. PRINT "        4       4       0100       12        C      1100"
  87. PRINT "        5       5       0101       13        D      1101"
  88. PRINT "        6       6       0110       14        E      1110"
  89. PRINT "        7       7       0111       15        F      1111"
  90. LOCATE 21,15:INPUT "Press RETURN for Menu ",dum$
  91. GOTO Main
  92. GOTO Main
  93. REM**************** End of Main Program ************************************
  94.  
  95. REM Converts a Hexadecimal Number to a Binary Number
  96.  
  97. SUB HEXBIN (hexa$) STATIC
  98. SHARED BinNum$
  99. l=LEN(hexa$)
  100. BinNum$=""
  101. FOR i=1 TO l
  102.  
  103. RESTORE HexData
  104. FOR x= 1 TO 16
  105. READ h$,b$
  106. IF MID$(hexa$,i,1)=h$ THEN BinNum$=BinNum$+ b$
  107. NEXT x
  108. NEXT i
  109.  
  110. HexData:
  111. DATA "0","0000","1","0001","2","0010","3","0011","4","0100","5","0101","6","0110","7","0111"
  112. DATA "8","1000","9","1001","A","1010","B","1011","C","1100","D","1101","E","1110","F","1111"
  113. END SUB
  114.  
  115. REM Converts a Binary Number to a Hexadecimal Number
  116.  
  117. SUB BINHEX (bina$) STATIC
  118. SHARED HexNum$
  119. l=LEN(bina$)
  120. HexNum$=""
  121. FOR i=1 TO l STEP 4
  122. RESTORE BinData
  123. FOR x = 1 TO 16
  124. READ h$,b$
  125. IF MID$(bina$,i,4)=b$ THEN HexNum$=HexNum$+h$
  126. NEXT x
  127. NEXT i
  128.  
  129. BinData:
  130. DATA "0","0000","1","0001","2","0010","3","0011","4","0100","5","0101","6","0110","7","0111"
  131. DATA "8","1000","9","1001","A","1010","B","1011","C","1100","D","1101","E","1110","F","1111"
  132. END SUB
  133.  
  134.